package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pojo.user.ChargeUser;
import com.zhentao.model.pojo.user.history.ChargePointsHistoryChange;
import com.zhentao.model.pojo.user.history.param.UserHistoryParam;
import com.zhentao.model.pojo.user.history.vo.UserHistoryVo;
import com.zhentao.service.ChargePointsHistoryChangeService;
import com.zhentao.mapper.ChargePointsHistoryChangeMapper;
import com.zhentao.service.ChargeUserService;
import com.zhentao.user.ChargeUserThreadUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author lenovo
* @description 针对表【charge_points_history_change】的数据库操作Service实现
* @createDate 2024-12-23 10:08:35
*/
@Service
public class ChargePointsHistoryChangeServiceImpl extends ServiceImpl<ChargePointsHistoryChangeMapper, ChargePointsHistoryChange>
    implements ChargePointsHistoryChangeService{
    @Autowired
    private ChargeUserService userService;


    /**
     * @description  查询积分记录
     * @author 振涛教育_小Q
     * @date    2024/12/23 16:34
     * @param
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult selectPointsChange(UserHistoryParam param) {
        // 分页处理
        Page<ChargePointsHistoryChange> changePage = new Page<>(param.getPageNum(), param.getPageSize());
        // 获取当前登录用户id
        ChargeUser user = ChargeUserThreadUtils.getChargeUserThreadLocal();
        if(user == null) return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        this.page(changePage,Wrappers.<ChargePointsHistoryChange>lambdaQuery()
                .eq(StringUtils.checkValNotNull(user.getId()), ChargePointsHistoryChange::getUserId, user.getId())
                .orderByDesc(ChargePointsHistoryChange::getCreateTime));

        List<ChargePointsHistoryChange> changes = changePage.getRecords();

        for (int i = 0; i < changes.size(); i++) {
            ChargePointsHistoryChange change = changes.get(i);
            UserHistoryVo vo = new UserHistoryVo();
            BeanUtils.copyProperties(change,vo);
            if(user.getId() != null){
                ChargeUser chargeUser = userService.getById(user.getId());
                vo.setNickName(chargeUser.getNickName());
            }
            changes.set(i,vo);
        }
        return ResponseResult.okResult(changePage);
    }
}




